// pages/goodsDetail/goodsDetail.js
import {
    request
} from "../../request/request.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        carts: 0,
        nihaonum:0,
        num: 0,
        goodsObj: [],
        isCollect: false

    },
    // 商品对象
    GoodsInfo: {},
    async getGoodsDetail(goods_id) {
        const goodsObj = await request({
            url: "https://api-hmugo-web.itheima.net/api/public/v1/goods/detail",
            data: {
                goods_id
            }
        })
        this.GoodsInfo = goodsObj;
        let collect = wx.getStorageSync('collect') || []
        let isCollect = collect.some(v => v.data.message.goods_id === this.GoodsInfo.data.message.goods_id)
        this.setData({
            // 优化页面渲染的属性(只要自己需要的)。 
            goodsObj: {
                data: {
                    message: {
                        goods_name: goodsObj.data.message.goods_name,
                        goods_price: goodsObj.data.message.goods_price,
                        goods_introduce: goodsObj.data.message.goods_introduce,
                        pics: goodsObj.data.message.pics
                    }
                }
            },
            isCollect
        })

    },
    // 点击图片放大预览
    hao(e) {
        const urls = this.GoodsInfo.data.message.pics.map(v => v.pics_mid)
        const current = e.currentTarget.dataset.url;
        wx.previewImage({
            current: current,
            urls: urls
        })
    },
    //点击购物车
    hanleCartAdd() {
        let cart = wx.getStorageSync("cart") || []
        let index = cart.findIndex(v => v.data.message.goods_id === this.GoodsInfo.data.message.goods_id)
        if (index === -1) {
            this.GoodsInfo.num = 1
            cart.push(this.GoodsInfo)
            this.setData({
                nihaonum: cart.length
            });
        } else {
            cart[index].num++
        }
        wx.setStorageSync("cart", cart)

        // this.setData({
        //     carts: cart.length
        // });
        wx.showToast({
            title: '加入成功',
            icon: "success",
            mask: true
        })
    },
    //点击收藏
    haohao() {
        let isCollect = false
        let collect = wx.getStorageSync('collect') || [];
        let index = collect.findIndex(v => v.data.message.goods_id === this.GoodsInfo.data.message.goods_id)
        if (index !== -1) {
            collect.splice(index, 1)
            isCollect = false
            wx.showToast({
                title: '收藏取消',
                icon: "splice",
                mask: true
            })
            this.setData({
                carts: collect.length
            })
        } else {
            collect.push(this.GoodsInfo);
            isCollect = true
            wx.showToast({
                title: '收藏成功',
                icon: "success",
                mask: true
            })
            this.setData({
                carts: collect.length
            })
        }
        wx.setStorageSync("collect", collect)
        this.setData({
            isCollect
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const {
            goods_id
        } = options
        this.getGoodsDetail(goods_id)


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let pages = getCurrentPages()
        let currentPage = pages[pages.length - 1]
        let options = currentPage.options
        const {
            goods_id
        } = options
        this.getGoodsDetail(goods_id)

        const cart = wx.getStorageSync('cart') || [];
        this.setData({
            nihaonum: cart.length
        });

        const carts = wx.getStorageSync('collect') || [];
        this.setData({
            carts: carts.length
        });

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})